package com.edwin.testingclass.commonutil;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;


public class ConfigUtil {
	private static Map<String, String> map=new HashMap<String,String>();
	private static ConfigUtil configUtil=null;
	
	private ConfigUtil() {
	}
	
	private ConfigUtil(String confile){
			Properties properties=new Properties();
			FileInputStream fileInputStream=null;
			try {
				fileInputStream=new FileInputStream(confile);
				properties.load(fileInputStream);
				Iterator<Entry<Object,Object>> iterator=properties.entrySet().iterator();
				while(iterator.hasNext()){
					Entry<Object,Object> entry=iterator.next();
					Object keyObject=entry.getKey();
					Object valueObject=entry.getValue();
					map.put(keyObject.toString(), valueObject.toString());
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				try {
					fileInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
	}
	
	//单例
	public static ConfigUtil getInstance(String confile){
		if(configUtil==null){
			configUtil=new ConfigUtil(confile);
		}
		return configUtil;
	}
	
	public static String getMapValue(String key) {
		return map.get(key);
	}
}
